<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

<?=form_hidden("add_id", $this->input->post("addId"))?>
<?=form_hidden("add_ins_id", $this->input->post("insId"))?>

<div class="form-group">
    <?=form_label($this->lang->line("NAME"), "add_name", array("class" => "control-label"))?>
    <?=form_input("add_name", (property_exists($institute_address, "add_name") ? $institute_address->add_name : null), "placeholder=\"".$this->lang->line("NAME")."\" class=\"form-control\" id=\"add_name\"")?>
</div>

<div class="form-group">
    <?=form_label($this->lang->line("CITY"), "add_city_id", array("class" => "control-label"))?>
    <?=form_input("add_city_id", $institute_address->add_city_id, "style=\"width:100%;\" id=\"add_city_id\"")?>
    <script>
        $(function() {
            $('#add_city_id').select2({
                allowClear: true,
                placeholder: '<?=$this->lang->line("CITY")?>',
                minimumInputLength: 2,
                ajax: {
                    url: '<?=site_url("admin_lte/get_city")?>',
                    cache: false,
                    dataType: 'json',
                    data: function (term, page) {
                        return {
                            q: term
                        };
                    },
                    results: function (data, page) {
                        return {results: data, more: false};
                    }
                },
                initSelection: function(element, callback) {
                    var id=$(element).val();
                    if (id!=="") {
                        $.ajax('<?=site_url("admin_lte/get_city")?>', {
                            data: {
                                id: id
                            },
                            cache: false,
                            dataType: "json"
                        }).done(function(data) { callback(data); });
                    }
                },
                formatSelection: function(a, b, c) {
                    if($.isArray(a)) {
                        return a[0].text;
                    }
                    return a.text;
                },
                escapeMarkup: function (m) { return m; }
            });
        });
    </script>
</div>

<div class="form-group">
    <?=form_label($this->lang->line("POSTAL_CODE"), "add_postal_code", array("class" => "control-label"))?>
    <?=form_input("add_postal_code", (property_exists($institute_address, "add_postal_code") ? $institute_address->add_postal_code : null), "placeholder=\"".$this->lang->line("POSTAL_CODE")."\" class=\"form-control\" id=\"add_postal_code\"")?>
</div>

<div class="form-group">
    <?=form_label($this->lang->line("ADDRESS")." (<span id=\"marker\"><span class=\"glyphicon glyphicon-map-marker\" title=\"".$this->lang->line("SEE_ON_MAP")."\"></span> ".$this->lang->line("SEE_ON_MAP")."</span>)", "add_address", array("class" => "control-label"))?>
    <?=form_input("add_address", (property_exists($institute_address, "add_address") ? $institute_address->add_address : null), "placeholder=\"".$this->lang->line("ADDRESS")."\" class=\"form-control\" id=\"add_address\"")?>
</div>

<div class="form-group">
    <?=form_label($this->lang->line("LATITUDE"), "add_latitude", array("class" => "control-label"))?>
    <?=form_input("add_latitude", (property_exists($institute_address, "add_latitude") ? $institute_address->add_latitude : null), "placeholder=\"".$this->lang->line("LATITUDE")."\" class=\"form-control\" id=\"add_latitude\"")?>
</div>

<div class="form-group">
    <?=form_label($this->lang->line("LONGITUDE"), "add_longitude", array("class" => "control-label"))?>
    <?=form_input("add_longitude", (property_exists($institute_address, "add_longitude") ? $institute_address->add_longitude : null), "placeholder=\"".$this->lang->line("LONGITUDE")."\" class=\"form-control\" id=\"add_longitude\"")?>
</div>

<script>
    $(function() {
        if($('#add_address').length) {
            $('#add_address').blur(function() {
                var geocoder = new google.maps.Geocoder();
                geocoder.geocode( { address: $(this).val() }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        $('#add_latitude').val(results[0].geometry.location.lat());
                        $('#add_longitude').val(results[0].geometry.location.lng());
                    }
                })
            });
        }

        $('#marker').addClass('cp').click(function() {
            if($('#add_latitude').val() && $('#add_longitude').val()) {
                $('#studySecondaryDisplayModal .modal-title').html('<?=$this->lang->line("GOOGLE_MAP")?>');
                $('#studySecondaryDisplayModal .modal-body').height(500);
                $('#studySecondaryDisplayModal').modal('toggle');

                $('#studySecondaryDisplayModal').on('shown.bs.modal', function () {
                    var mapOptions = {
                        center: new google.maps.LatLng($('#add_latitude').val(), $('#add_longitude').val()),
                        zoom: 14,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    var map = new google.maps.Map($('#studySecondaryDisplayModal .modal-body')[0], mapOptions);
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng($('#add_latitude').val(), $('#add_longitude').val())
                    });
                    marker.setMap(map);
                });
            }
        });

    });
</script>